package com.ssfGame.puremvc.view.module.gameOver
{
	import api.event.CustomEvent;
	
	import com.ssfGame.puremvc.StaticConst;
	import com.ssfGame.puremvc.view.application.SceneLayerMediator;
	import com.ssfGame.puremvc.view.module.ModuleVC;
	import com.ssfGame.puremvc.view.module.gameOver.vc.GameOverVC;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class GameOverMediator extends Mediator
	{
		public static const NAME:String="GameOverMediator";
		private var _gameOverVC:GameOverVC;
		public function GameOverMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
		}
		override public function listNotificationInterests():Array{
			return [
				StaticConst.N_M_GAME_OVER_ADD,
				StaticConst.N_M_GAME_OVER_DEL
			]
		}
		override public function handleNotification(notification:INotification):void{
			switch(notification.getName()){
				case StaticConst.N_M_GAME_OVER_ADD:
					sendNotification(StaticConst.N_M_SCENE_LAYER_DEL,null,SceneLayerMediator.DEL_ALL);
					if(_gameOverVC==null){
						_gameOverVC=new GameOverVC();
						_gameOverVC.addEventListener(ModuleVC.TO_MEDIATOR,recEvent);
					}
					sendNotification(StaticConst.N_M_SCENE_LAYER_ADD,_gameOverVC);
					break;
				case StaticConst.N_M_GAME_OVER_DEL:
					delGameOverVC();
					break;
			}
		}
		private function recEvent(e:CustomEvent):void{
			switch(e.type){
				case GameOverVC.USER_CHANGE:
					delGameOverVC();
					sendNotification(StaticConst.N_C_INIT_PVP_API);;
					break
				case GameOverVC.START_GAME:
					delGameOverVC();
					sendNotification(StaticConst.N_M_GAME_ADD);;
					break
			}
		}
		private function delGameOverVC():void{
			if(_gameOverVC!=null){
				sendNotification(StaticConst.N_M_SCENE_LAYER_DEL,_gameOverVC);
				_gameOverVC.removeEventListener(ModuleVC.TO_MEDIATOR,recEvent);
				_gameOverVC.gc();
				_gameOverVC=null;
			}
		}
	}
}